package DemoReview;

/**
 * 除自身以外数组的乘积
 */
public class LeetCode238 {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] arr = new int[n];
        int[] dp1 = new int[n];
        int[] dp2 = new int[n];
        dp1[0] = dp2[n - 1] = 1;
        for (int i = 1; i < n ; i++)
            dp1[i] = dp1[i - 1] * nums[i - 1];
        for (int i = n - 2; i >= 0; i--)
            dp2[i] = dp2[i + 1] * nums[i + 1];
        for (int i = 0; i < n; i++)
            arr[i] = dp1[i] * dp2[i];
        return arr;
    }
}
